package org.autumn.review.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class WaitNotifyMethod {

    static final Lock lock = new Lock();
    public static void main(String[] args) {

    }

    private static void testWaitSet() {
        Thread t1 = new Thread(() -> {
            synchronized (lock){
                try {
                    log.info("");
                    lock.wait(1000);
                    log.info("");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t1");
        t1.start();


        Thread t2 = new Thread(() -> {
            synchronized (lock){
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.notify();
            }
        }, "t2");
        t2.start();
    }

    /**
     * a simple demo for wait and notify method
     */
    public static void waitNotifyDemo() {
        Thread t1 = new Thread(() -> {
            synchronized (WaitNotifyMethod.class) {
                try {
                    WaitNotifyMethod.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.info("i am awakened by t2");
            }
        }, "t1");
        t1.start();


        Thread t2 = new Thread(() -> {
            synchronized (WaitNotifyMethod.class) {
                log.info("i will wake up t1 thread");
                WaitNotifyMethod.class.notify();
            }
        }, "t2");
        t2.start();
    }
}


class Lock{

}